﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Server.Util;
using Server.Models;
using System.Data;

namespace Server.UserService
{
    public class CUserDAL
    {
        CacheUtil cacheCtrl = new CacheUtil();
        DBUtil dbCtrl = new DBUtil();
        /// <summary>
        /// 登录时通过用户名获取用户信息
        /// </summary>
        /// <param name="LoginName"></param>
        /// <returns></returns>
        public UserInfo GetUserByName(string LoginName)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_User:LoginName:" + LoginName);
                if (string.IsNullOrEmpty(tmpStr))
                {
                    CacheUserByName();
                    tmpStr = (string)cacheCtrl.CacheGet("Manage_User:LoginName:" + LoginName);
                }
                UserInfo user = JsonConvert.DeserializeObject<UserInfo>(tmpStr);
                return user;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 登录时保存在线信息
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="id"></param>
        public void SaveOnline(string Token, int id)
        {
            try
            {
                cacheCtrl.CacheSet("Client_Online:Token:" + Token, id);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 检查用户Token
        /// </summary>
        /// <param name="Token"></param>
        /// <returns></returns>
        public bool CheckUser(string Token)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Client_Online:Token:" + Token);
                if (string.IsNullOrEmpty(tmpStr))
                    return false;
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetDevice(int id)
        {
            try
            {
                string sqlstr = " select * from Manage_DeviceInfo where id = " + id;
                var dt = dbCtrl.Query(sqlstr);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void SaveDeviceSession(string session ,int id)
        {
            try
            {
                string sqlstr = " update Manage_DeviceInfo set DeviceSession = " + session +" where id = "+id;
                dbCtrl.Exec(sqlstr);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public UserInfo GetUserByToken(string Token)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Client_Online:Token:" + Token);
                if (string.IsNullOrEmpty(tmpStr))
                    throw new Exception("用户已掉线,请重新登录!");
                int id = JsonConvert.DeserializeObject<int>(tmpStr);
                return this.GetUserByID(id);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public UserInfo GetUserByID(int id)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_User:ID:" + id);
                if (string.IsNullOrEmpty(tmpStr))
                {
                    CacheUserByID();
                    tmpStr = (string)cacheCtrl.CacheGet("Manage_User:ID:" + id);
                }
                UserInfo user = JsonConvert.DeserializeObject<UserInfo>(tmpStr);
                return user;
            }
            catch (Exception)
            {
                throw;
            }

        }
        public void CacheUserByID()
        {
            try
            {
                string sqlstr = " select * from Manage_User ";
                var dt = dbCtrl.Query(sqlstr);
                UserInfo[] user = new UserInfo[dt.Rows.Count];
                for (int i = 0; i < user.Length; i++)
                {
                    var dr = dt.Rows[i];
                    user[i] = new UserInfo()
                    {
                        Id = Convert.ToInt32(dr["id"]),
                        LoginName = dr["LoginName"].ToString(),
                        Password = dr["Password"].ToString(),
                        Sault = dr["Sault"].ToString(),
                        State = Convert.ToInt32(dr["State"]),
                        MCompanyId = Convert.ToInt32(dr["MCompanyId"]),
                        MCompanyName = dr["MCompanyName"].ToString(),
                        DeviceId = Convert.ToInt32(dr["DeviceId"]),
                        DeviceSession = dr["DeviceSession"].ToString(),
                        SessionExpired = Convert.ToDateTime(dr["SessionExpired"]),
                        ExpireTime = Convert.ToDateTime(dr["ExpireTime"]),
                        CompanyName = dr["CompanyName"].ToString(),
                        ContactName = dr["ContactName"].ToString(),
                        TelNum = dr["TelNum"].ToString(),
                        MobNum = dr["MobNum"].ToString(),
                        Address = dr["Address"].ToString(),
                        ProvinceId = Convert.ToInt32(dr["ProvinceId"]),
                        ProvinceName = dr["ProvinceName"].ToString(),
                        CityId = Convert.ToInt32(dr["CityId"]),
                        CityName = dr["CityName"].ToString(),
                        DistrictId = Convert.ToInt32(dr["DistrictId"]),
                        DistrictName = dr["DistrictName"].ToString(),
                        TaxNum = dr["TaxNum"].ToString(),
                        BankName = dr["BankName"].ToString(),
                        BankNum = dr["BankNum"].ToString(),
                        SBProvinceId = Convert.ToInt32(dr["SBProvinceId"]),
                        SBProvinceName = dr["SBProvinceName"].ToString(),
                        SBCityId = Convert.ToInt32(dr["SBCityId"]),
                        SBCityName = dr["SBCityName"].ToString(),
                        SBId = Convert.ToInt32(dr["SBId"]),
                        SBName = dr["SBName"].ToString(),
                        TradeBigId = dr["TradeBigId"].ToString(),
                        TradeBigName = dr["TradeBigName"].ToString(),
                        TradeMidId = dr["TradeMidId"].ToString(),
                        TradeMidName = dr["TradeMidName"].ToString(),
                        TradeSmallId = dr["TradeSmallId"].ToString(),
                        TradeSmallName = dr["TradeSmallName"].ToString()

                    };
                    cacheCtrl.CacheSet("Manage_User:ID:" + user[i].Id, user[i]);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CacheUserByName()
        {
            try
            {
                string sqlstr = " select * from Manage_User ";
                var dt = dbCtrl.Query(sqlstr);
                UserInfo[] user = new UserInfo[dt.Rows.Count];
                for (int i = 0; i < user.Length; i++)
                {
                    var dr = dt.Rows[i];
                    user[i] = new UserInfo()
                    {
                        Id = Convert.ToInt32(dr["id"]),
                        LoginName = dr["LoginName"].ToString(),
                        Password = dr["Password"].ToString(),
                        Sault = dr["Sault"].ToString(),
                        State = Convert.ToInt32(dr["State"]),
                        MCompanyId = Convert.ToInt32(dr["MCompanyId"]),
                        MCompanyName = dr["MCompanyName"].ToString(),
                        DeviceId = Convert.ToInt32(dr["DeviceId"]),
                        DeviceSession = dr["DeviceSession"].ToString(),
                        SessionExpired = Convert.ToDateTime(dr["SessionExpired"]),
                        ExpireTime = Convert.ToDateTime(dr["ExpireTime"]),
                        CompanyName = dr["CompanyName"].ToString(),
                        ContactName = dr["ContactName"].ToString(),
                        TelNum = dr["TelNum"].ToString(),
                        MobNum = dr["MobNum"].ToString(),
                        Address = dr["Address"].ToString(),
                        ProvinceId = Convert.ToInt32(dr["ProvinceId"]),
                        ProvinceName = dr["ProvinceName"].ToString(),
                        CityId = Convert.ToInt32(dr["CityId"]),
                        CityName = dr["CityName"].ToString(),
                        DistrictId = Convert.ToInt32(dr["DistrictId"]),
                        DistrictName = dr["DistrictName"].ToString(),
                        TaxNum = dr["TaxNum"].ToString(),
                        BankName = dr["BankName"].ToString(),
                        BankNum = dr["BankNum"].ToString(),
                        SBProvinceId = Convert.ToInt32(dr["SBProvinceId"]),
                        SBProvinceName = dr["SBProvinceName"].ToString(),
                        SBCityId = Convert.ToInt32(dr["SBCityId"]),
                        SBCityName = dr["SBCityName"].ToString(),
                        SBId = Convert.ToInt32(dr["SBId"]),
                        SBName = dr["SBName"].ToString(),
                        TradeBigId = dr["TradeBigId"].ToString(),
                        TradeBigName = dr["TradeBigName"].ToString(),
                        TradeMidId = dr["TradeMidId"].ToString(),
                        TradeMidName = dr["TradeMidName"].ToString(),
                        TradeSmallId = dr["TradeSmallId"].ToString(),
                        TradeSmallName = dr["TradeSmallName"].ToString()
                        
                    };
                    cacheCtrl.CacheSet("Manage_User:LoginName:" + user[i].LoginName, user[i]);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
